package game.module.itemnew.dao;

import game.module.itemnew.beans.ItemStorage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;

@Repository
public class ItemDao {

	private LinkedBuffer buffer;

	@PostConstruct
	public void init() {
		buffer = LinkedBuffer.allocate(65536);
	}

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void saveItem(final int roleId, ItemStorage itemStorage) {

		final InputStream inputStream;
		try {
			byte[] content = ProtostuffIOUtil.toByteArray(itemStorage, ItemStorage.getSchema(), buffer);
			inputStream = new ByteArrayInputStream(content);
		} finally {
			buffer.clear();
		}
		jdbcTemplate.update("update item set item_blob = ? where role_id = ?", new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setBlob(1, inputStream);
				ps.setInt(2, roleId);
			}
		});
	}

	public ItemStorage getItems(int roleId) {
		List<ItemStorage> goodList = jdbcTemplate.query("select item_blob from item where role_id = ?", new Object[] { roleId }, new RowMapper<ItemStorage>() {
			@Override
			public ItemStorage mapRow(ResultSet rs, int rowNum) throws SQLException {
				ItemStorage gl = new ItemStorage();
				InputStream is = rs.getBinaryStream(1);
				try {
					ProtostuffIOUtil.mergeFrom(is, gl, ItemStorage.getSchema());
				} catch (IOException e) {
					e.printStackTrace();
				}
				return gl;
			}
		});
		if (goodList.size() == 0) {
			return null;
		}
		return goodList.get(0);
	}

	public void batchUpdateItem(final List<ItemStorage> itemList, final List<Integer> roleIdList) {
		jdbcTemplate.batchUpdate("update item set item_blob = ? where role_id = ?", new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ItemStorage itemStorage = itemList.get(i);
				Integer roleId = roleIdList.get(i);
				if (itemStorage != null) {
					byte[] content = ProtostuffIOUtil.toByteArray(itemStorage, ItemStorage.getSchema(), buffer);
					InputStream inputStream = new ByteArrayInputStream(content);
					ps.setBlob(1, inputStream);
					ps.setInt(2, roleId);
				}
			}

			@Override
			public int getBatchSize() {
				return itemList.size();
			}
		});
	}

	public int initItemRecord(int roleId) {
		return jdbcTemplate.update("insert into item(role_id) values(?)", roleId);
	}

	public void removeItemByRoleId(int roleId) {
		jdbcTemplate.update("delete from item where role_id = ?", roleId);
	}

}
